package cn.iocoder.yudao.module.pypm.controller.admin.linkinfo;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pypm.controller.admin.linkinfo.vo.*;
import cn.iocoder.yudao.module.pypm.dal.dataobject.linkinfo.LinkinfoDO;
import cn.iocoder.yudao.module.pypm.service.linkinfo.LinkinfoService;

@Tag(name = "管理后台 - 关联信息")
@RestController
@RequestMapping("/pypm/linkinfo")
@Validated
public class LinkinfoController {

    @Resource
    private LinkinfoService linkinfoService;

    @PostMapping("/create")
    @Operation(summary = "创建关联信息")
    @PreAuthorize("@ss.hasPermission('pypm:linkinfo:create')")
    public CommonResult<Long> createLinkinfo(@Valid @RequestBody LinkinfoSaveReqVO createReqVO) {
        return success(linkinfoService.createLinkinfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新关联信息")
    @PreAuthorize("@ss.hasPermission('pypm:linkinfo:update')")
    public CommonResult<Boolean> updateLinkinfo(@Valid @RequestBody LinkinfoSaveReqVO updateReqVO) {
        linkinfoService.updateLinkinfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除关联信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pypm:linkinfo:delete')")
    public CommonResult<Boolean> deleteLinkinfo(@RequestParam("id") Long id) {
        linkinfoService.deleteLinkinfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得关联信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pypm:linkinfo:query')")
    public CommonResult<LinkinfoRespVO> getLinkinfo(@RequestParam("id") Long id) {
        LinkinfoDO linkinfo = linkinfoService.getLinkinfo(id);
        return success(BeanUtils.toBean(linkinfo, LinkinfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得关联信息分页")
    @PreAuthorize("@ss.hasPermission('pypm:linkinfo:query')")
    public CommonResult<PageResult<LinkinfoRespVO>> getLinkinfoPage(@Valid LinkinfoPageReqVO pageReqVO) {
        PageResult<LinkinfoDO> pageResult = linkinfoService.getLinkinfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LinkinfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出关联信息 Excel")
    @PreAuthorize("@ss.hasPermission('pypm:linkinfo:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLinkinfoExcel(@Valid LinkinfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LinkinfoDO> list = linkinfoService.getLinkinfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "关联信息.xls", "数据", LinkinfoRespVO.class,
                        BeanUtils.toBean(list, LinkinfoRespVO.class));
    }

}